(function () {

    // 修改密码
    function update(pwd) {
        swal({
            title: '确认要修改？',
            type: 'warning',
            showCancelButton: true,
            cancelButtonText: '取消',
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定!'
        }).then(function (rs) {
            if (rs.value) {
                $.ajax({
                    url: base + '/teacher/updatePwd',
                    type: 'post',
                    data: {pwd: pwd},
                    success: function (result) {
                        if (result == 1) {
                            swal({
                                title: '修改成功！'
                            })
                            window.setTimeout(function () {
                                window.location.href = base + '/login';
                            }, 1000)
                        } else {
                            swal({
                                title: '修改失败！'
                            })
                        }
                    }, error: function () {
                        swal({
                            title: '系统繁忙！'
                        })
                    }
                })
            }
        })
    }

    $('#btn-update').click(function () {

        var pwd = $('#pwd').val();
        var rePwd = $('#rePwd').val();
        var confirmPwd = $('#confirmPwd').val();
        // 密码一致性校验
        rePwd = rePwd.trim();
        confirmPwd = confirmPwd.trim();
        if (rePwd != confirmPwd) {
            swal({
                title: '密码不一致'
            })
            return;
        }
        if (!/^[\d\w]{3,16}$/.test(rePwd)) {
            swal({
                text: '密码应该由3-16位的数字字母组成'
            })
            return;
        }

        // 校验密码正确性
        $.ajax({
            url: base + '/teacher/checkPwd',
            type: 'post',
            data: {pwd: pwd},
            success: function (res) {
                res = JSON.parse(res)
                if (res.valid) {
                    update(rePwd);
                } else {
                    swal({
                        text: '原密码错误！'
                    });
                }
            }, error: function () {
                swal({
                    text: '系统繁忙！'
                });
            }
        })

    });


    // 基本信息
    $('#updateTeacherForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            te_name: {
                message: '姓名验证失败',
                validators: {
                    notEmpty: {
                        message: '姓名不能为空'
                    },
                }
            },
            te_login_name: {
                message: '登录名验证失败',
                validators: {
                    notEmpty: {
                        message: '登录名不能为空'
                    },
                    remote: {   //{'valid':true |false }
                        url: '/teacher/checkLoginName',
                        message: "该用户名已经存在！",
                        delay: 500,
                        type: 'post',
                    }
                }
            },
            te_age: {
                message: '年龄验证失败',
                validators: {
                    notEmpty: {
                        message: '年龄不能为空'
                    }, digits: {
                        message: "只能包含数字"
                    }, greaterThan: {
                        value: 0,
                        message: "年龄不能小于0岁"
                    }, lessThan: {
                        value: 200,
                        message: "年龄不能大于200岁"
                    }
                }
            },
            password: {
                message: "密码验证失败",
                validators: {
                    notEmpty: {
                        message: '年龄不能为空'
                    }, regexp: {
                        regexp: /^[\d\w]{3,10}$/,
                        message: "密码不合法，密码应该由3-10位字母、数字组成"
                    }
                }
            }
        }
    });

    function updateInfo() {
        //表单提交的方法、比如ajax提交
        var data = $('#updateTeacherForm').serializeJSON();
        console.log(JSON.stringify(data))
        $.ajax({
            url: '/teacher/update',
            type: 'post',
            data: data,
            success: function (result) {
                if (result == 1) {
                    swal({
                        title: '修改成功！'
                    })
                } else {
                    swal({
                        title: '修改失败！'
                    })
                }
            }, error: function () {
                swal({
                    title: '系统繁忙！'
                })
            }
        });
    }

    $('#updateTeacher').click(function () {
        var bootstrapValidator = $('#updateTeacherForm').data('bootstrapValidator');
        //手动触发验证
        bootstrapValidator.validate();
        if (bootstrapValidator.isValid()) {
            swal({
                title: '确定?',
                text: "确定要修改吗!",
                type: 'warning',
                showCancelButton: true,
                cancelButtonText: '取消',
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: '确定!'
            }).then(function (result) {
                if (result.value) {
                    updateInfo();
                }
            });
        }

    });


    // 头像
    $("#file-4").fileinput({
        theme: "explorer", //主题
        language: 'zh',
        browseLabel: '浏览',
        dropZoneTitle: '把文件拖到这里..',
        removeLabel: '删除',
        cancelLabel: '取消',
        uploadLabel: '上传',
        uploadUrl: base + "/teacher/uploadImg", // 上传请求路径
        allowedFileExtensions: ['jpg', 'gif', 'png', 'jpeg', 'pdf'],//允许上传的文件后缀
        uploadAsync: false, //是否允许异步上传
        showUpload: true,//是否显示上传按钮
        showCaption: false,//是否显示容器dropZoneEnabled: false,//是否显示拖拽区域
        removeFromPreviewOnError: true,//是否移除校验文件失败的文件
        uploadExtraData: function (previewId, index) {   //额外参数 返回json数组
            return {
                'id': 1  // <span style="font-family:Arial, Helvetica, sans-serif;">commId 为全局变量，可以给控件上传额外参数  </span>
            };
        },
        layoutTemplates: {    //取消上传按钮
            actionUpload: '',
        },
        showPreview: true,      //显示预览
        minFileCount: 1,   //最低文件数量
        maxFileCount: 1,   //最多文件数量
        maxFileSize: 1024,  //允许文件上传大小
        overwriteInitial: true,
        previewFileIcon: '<i class="fa fa-file"></i>',
        initialPreviewAsData: true, // defaults markup
        preferIconicPreview: false, // 是否优先显示图标  false 即优先显示图片
        previewFileIconSettings: { // configure your icon file extensions
            'pdf': '<i class="fa fa-file-pdf-o text-danger"></i>',
            'jpg': '<i class="fa fa-file-photo-o text-danger"></i>',
            'gif': '<i class="fa fa-file-photo-o text-muted"></i>',
            'png': '<i class="fa fa-file-photo-o text-primary"></i>',
            'jpeg': '<i class="fa fa-file-photo-o text-primary"></i>'
        },
    });
    $("#file-4").fileinput();
    $("#file-4").on("filebatchuploadsuccess", function (event, data, previewId, index) {
        if (data.response == true) {
            alert("上传成功")
        }
        var result = data.response.result;//接收后台返回的数据
    });


})()